<?php
header('Content-Type:text/html;charset=utf-8');

class Work extends CI_Controller {
	
	function index()
	{
		
		$this->db->where('mode',1);
		$count = $this->db->count_all_results('work');

		$config['base_url'] = 'http://darenshow.lovenest.cn/work/all/page/';
		$config['total_rows'] = $count;
		$config['per_page'] = 20; 
		$config['num_links'] = 6;
		$config['uri_segment'] = 4;
		$config['first_link'] = '最前页';
		$config['last_link'] = '最后页';
		$config['full_tag_open'] = '<div class="page-bar">';
		$config['full_tag_close'] = '</div>';
		
		$this->load->library('pagination');
		$this->pagination->initialize($config);

		$this->db->where('mode',1);
		$this->db->order_by("addtime", "desc");
		$data['results'] = $this->db->get('work', $config['per_page'], $this->uri->segment( 4 ));
		$data['pagination'] = $this->pagination->create_links();
		
		$this->load->view('work_index',$data);
	}
	
	function all($tid='')
	{
		
		$data['tid'] = $tid;
		$page_uri = $this->uri->segment(3);	//读取page的uri
		
		if ( $tid != '' and $page_uri != 'page' )	//所有作品/分类作品
		{
			$this->db->where('type',$tid );
		}
		$this->db->where('mode',1);
		$count = $this->db->count_all_results('work');
		if ( $count > 0 )
		{
			if ( $tid != '' and $page_uri != 'page' )
			{
				$segment = 5;
				$base = 'http://darenshow.lovenest.cn/work/all/' . $tid . '/page/';
			}
			else
			{
				$segment = 4;
				$base = 'http://darenshow.lovenest.cn/work/all/page/';
			}
			
			$config['base_url'] = $base;
			$config['total_rows'] = $count;
			$config['per_page'] = 20; 
			$config['num_links'] = 6;
			$config['uri_segment'] = $segment;
			$config['first_link'] = '最前页';
			$config['last_link'] = '最后页';
			$config['full_tag_open'] = '<div class="page-bar">';
			$config['full_tag_close'] = '</div>';
			
			$this->load->library('pagination');
			$this->pagination->initialize($config);

			if ( $tid != '' and $page_uri != 'page' )
			{
				$this->db->where('type',$tid );
			}
			$this->db->where('mode', 1);
			$this->db->order_by("addtime", "desc");
			$data['results'] = $this->db->get('work', $config['per_page'], $this->uri->segment( $segment ));
			$data['pagination'] = $this->pagination->create_links();

			//侧栏排行榜
			$top = $this->db->query('select * from work where mode = 1 order by cont DESC');
			$data['top'] = $top;
	
			$this->load->view('work_list',$data);
		
		}
		else
		{
			$data['tip'] = '<h3><a href="http://darenshow.lovenest.cn/#reg" class="red">宝宝作品火热征集中，快来参加吧！</a></h3>';
			$this->load->view('comming',$data);
		}
	}
	
	function show($wid='')
	{
		if ($wid){
			$nid = $wid + 1000;
			$pid = $wid - 1000;
			
			$query_prev = $this->db->query('select MIN(id) as id from work where mode=1 AND id> ' . $wid . ' AND id<' . $nid . ' order by id')->row()->id;
			$query_next = $this->db->query('select MAX(id) as id from work where mode=1 AND id< ' . $wid . ' AND id>' . $pid . ' order by id')->row()->id;
	
			$query = $this->db->query('select * from work where id=' . $wid . '')->row();
			$data['state'] = 1;//有数据
			$data['prev'] = $query_prev;
			$data['next'] = $query_next;
			$data['row'] = $query;
			if ( $query->mode == 1 )
			{
				$this->load->view('work_show',$data);
			}
			else
			{
				$data['tip'] = '作品正在审核中，请耐心等待……';
				$this->load->view('comming' , $data);
			}
		}
		else
		{
			$data['tip'] = '<h3><a href="http://darenshow.lovenest.cn/#reg" class="red">宝宝作品火热征集中，快来参加吧！</a></h3>';
			$this->load->view('comming',$data);
		}
	}
	
	function top()
	{
		$this->load->view('work_top');
	}
	
	function videoupload()
	{
		$this->load->view('work_videoupload');
	}
	
	function rule()
	{
		$this->load->view('work_rule');
	}
	
	function judges()
	{
		$this->load->view('work_judges');
	}
	
	function success($wid='')
	{
		$data['work_url'] = $wid;
		$this->load->view('work_success', $data);
	}

	function insert()
	{
		$data['tip'] = '<h3><a href="http://darenshow.lovenest.cn/work/all" class="red">报名已截止，为您喜欢的宝宝投上一票吧！</a></h3>';
		$this->load->view('comming',$data);
	}
	
/*
	function insert()
	{
		//验证表单

		if ( $_POST['name'] == '' )
		{
			alert("请填写宝宝姓名",0);
			return;
		}
		if ( $_POST['age'] == '' )
		{
			alert("请填写选择年龄",0);
			return;
		}
		if ( $_POST['contact'] == '' )
		{
			alert("请填写家长姓名",0);
		}
		if ( $_POST['phone'] == '' )
		{
			alert("请填写联系电话",0);
			return;
		}
		if ( $_POST['title'] == '' )
		{
			alert("请填写作品标题",0);
			return;
		}
		if ( str_length($_POST['summary'],'gb2312') > 1000 )
		{
			alert("作品描述请控制在1000字以内",0);
			return;
		}
		
		if ( $_POST['name'] != $this->session->userdata('name') && $_POST['phone'] != $this->session->userdata('phone') )//判断重复报名
		{
			//视频
			if ( $_POST['type'] == '0' )
			{
				if ( $_POST['content'] == '' )
				{
					alert("请填写视频地址",0);
					return;
				}
				date_default_timezone_set("PRC");
				$_POST['addtime'] = date("Y-m-j H:i:s");
				$_POST['ip'] = $this->input->ip_address();
				$_POST['source'] = $_SERVER['HTTP_REFERER'];
				//插入db
				$this->db->insert('work', $_POST);
				//插入session
				$newdata = array(
					 'name'		=> $_POST['name'],
					 'phone'	=> $_POST['phone']
				 );
				$this->session->set_userdata($newdata);
				redirect('/work/success/' . $this->db->insert_id());
				return;
			}
			
			//图片
			else
			{
				$config['upload_path'] = './uploads/photo/';
				$config['file_name'] = date("YmdHis");
				$config['allowed_types'] = 'gif|jpg';
				$config['max_size'] = '5120';
				$this->load->library('upload', $config);
				if (!$this->upload->do_upload('content'))
				{
					alert("图片格式不正确，图片5M以内，jpg或gif格式，请重新上传图片",0);
					return;
				}
				else
				{
					$upload_data = $this->upload->data();
					$_POST['content'] = '<img src="/uploads/photo/' . $upload_data['file_name'] . '" />';
					date_default_timezone_set("PRC");
					$_POST['addtime'] = date("Y-m-j H:i:s");
					$_POST['ip'] = $this->input->ip_address();
					
					$this->load->library('image_lib', $config); 
					
					//处理原图
					
					
					$config1['image_library'] = 'gd2';
					$config1['source_image'] = './uploads/photo/' . $upload_data['file_name'];
					$config1['maintain_ratio'] = TRUE;
					
					
					$size = GetImageSize($config1['source_image']);
					if ( $size[0] >750) $config1['width'] = 750;
					//if ( $size[1] >750) $config1['height'] = 750;
					
					$this->image_lib->initialize($config1);
					$this->image_lib->resize();
					$this->image_lib->clear();
					
					//生成缩略图
					$config2['image_library'] = 'gd2';
					$config2['source_image'] = './uploads/photo/' . $upload_data['file_name'];
					$config2['new_image'] = './uploads/photo/thumb/';
					$config2['maintain_ratio'] = TRUE;
					$config2['width'] = 128;
					$config2['height'] = 96;
					
					$this->image_lib->initialize($config2);
					$this->image_lib->resize();
					$this->image_lib->clear();
					
					$_POST['thumb'] = '/uploads/photo/thumb/' . $upload_data['file_name'];
					
					//插入db
					$this->db->insert('work', $_POST);
					
					//插入session
					$newdata = array(
						 'name'		=> $_POST['name'],
						 'phone'	=> $_POST['phone']
					 );
					$this->session->set_userdata($newdata);
					$this->session->set_userdata($newdata);
					redirect('/work/success/' . $this->db->insert_id());
					return;
				}
			}
		}
		else
		{
			alert("请勿重复报名，谢谢合作！",0);
			return;
		}
	
	}
*/	
}